package com.photopicker.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.photopicker.R;
import com.photopicker.util.ScreenUtil;


public class FlowIndicator extends View {
    private int count;
    private float space, radius;
    private int point_normal_color, point_selected_color;
    private int border_normal_color, border_selected_color;
    private float borderWidth;
    private float pRadius;
    private int selected = 0;

    private Paint paint_point;
    private Paint paint_border;

    public FlowIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.FlowIndicator);

        count = a.getInteger(R.styleable.FlowIndicator_point_count, 4);
        space = a.getDimension(R.styleable.FlowIndicator_point_space, ScreenUtil.dip2px(getContext(), 8f));
        radius = a.getDimension(R.styleable.FlowIndicator_point_radius, ScreenUtil.dip2px(getContext(), 4f));

        point_normal_color = a.getColor(
                R.styleable.FlowIndicator_point_normal_color, 0x000000);
        point_selected_color = a.getColor(
                R.styleable.FlowIndicator_point_selected_color, 0xffff07);

        borderWidth = a.getDimension(R.styleable.FlowIndicator_point_border_width, 0);
        pRadius = radius + borderWidth;

        paint_point = new Paint();
        paint_point.setAntiAlias(true);

        if (borderWidth > 0) {
            border_normal_color = a.getColor(
                    R.styleable.FlowIndicator_point_border_normal_color, 0x000000);
            border_selected_color = a.getColor(
                    R.styleable.FlowIndicator_point_border_selected_color, 0xffff07);

            paint_border = new Paint();
            paint_border.setAntiAlias(true);
            paint_border.setStrokeWidth(borderWidth);
        }
        a.recycle();
    }

    public void setSelection(int index) {
        this.selected = index;
        invalidate();
    }

    public void setCount(int count) {
        this.count = count;
        invalidate();
    }

    public void next() {
        if (selected < count - 1) {
            selected++;
        } else {
            selected = 0;
        }
        invalidate();
    }

    public void previous() {
        if (selected > 0) {
            selected--;
        } else {
            selected = count - 1;
        }
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float width = (getWidth() - ((radius * 2 * count) + (space * (count - 1)))) / 2.f;
        for (int i = 0; i < count; i++) {
            float cx = width + getPaddingLeft() + radius + i * (space + radius + radius);
            float cy = getHeight() / 2;
            if (i == selected) {
                paint_point.setColor(point_selected_color);
            } else {
                paint_point.setColor(point_normal_color);
            }
            canvas.drawCircle(cx, cy, radius, paint_point);

            if (paint_border != null) {
                if (i == selected) {
                    paint_border.setColor(border_selected_color);
                } else {
                    paint_border.setColor(border_normal_color);
                }
                canvas.drawCircle(cx, cy, pRadius, paint_border);
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWidth(widthMeasureSpec),
                measureHeight(heightMeasureSpec));
    }

    private int measureWidth(int measureSpec) {
        int result;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = (int) (getPaddingLeft() + getPaddingRight()
                    + (count * 2 * pRadius) + (count - 1) * pRadius + 1);
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    private int measureHeight(int measureSpec) {
        int result;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = (int) (2 * pRadius + getPaddingTop() + getPaddingBottom() + 1);
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

}
